package com.ecloud.gardensterminal.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecloud.gardensterminal.controller.dto.GardensThresholdDto;
import com.ecloud.gardensterminal.controller.dto.GardensThresholdQueryDto;
import com.ecloud.gardensterminal.dao.GardensThresholdMapper;
import com.ecloud.gardensterminal.domain.GardensThreshold;
import com.ecloud.gardensterminal.server.GardensThresholdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Squbi
 */
@Service
public class GardensThresholdServiceImpl implements GardensThresholdService {
    @Autowired
    private GardensThresholdMapper thresholdMapper;

    @Override
    public void insertThreshold(GardensThresholdDto dto) {
        thresholdMapper.insert(dto);
    }

    @Override
    public List<GardensThreshold> getThresholds() {
        return thresholdMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public GardensThreshold getThreshold(Integer deviceId) {
        return thresholdMapper.selectOne(new QueryWrapper<GardensThreshold>().eq("device_id", deviceId));
    }

    @Override
    public List<GardensThreshold> queryThreshold(GardensThresholdQueryDto dto) {
        dto.setPageIndex(dto.getPageSize() * (dto.getPageIndex() - 1));
        return thresholdMapper.queryList(dto);
    }

    @Override
    public void updateThreshold(GardensThresholdDto dto) {
        Integer thresholdId = getThreshold(dto.getDeviceId()).getThresholdId();
        GardensThreshold gardensThreshold = new GardensThreshold(dto);
        gardensThreshold.setThresholdId(thresholdId);
        thresholdMapper.updateById(gardensThreshold);
    }

    @Override
    public void deleteThreshold(Integer deviceId) {
        GardensThreshold threshold = getThreshold(deviceId);
        thresholdMapper.deleteById(threshold.getThresholdId());
    }
}
